home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / technote / tchntstc.sit / Technical Notes Stack 3.2.1 / Technical Notes Stack 3.2.1 / card_55194.txt < prev    next >
Encoding:
Text File  |  1990-01-08  |  15.8 KB  |  286 lines

  1. -- card: 55194 from stack: in.1
  2. -- bmap block id: 0
  3. -- flags: 0000
  4. -- background id: 2711
  5. -- name: 
  6.  
  7.  
  8. -- part contents for background part 9
  9. ----- text -----
  10. #206:    Space Aliens Ate My Mouse
  11.          (ADBΓÇôThe Untold Story)
  12.  
  13. Revised by:    Cameron Birse                                       October 1989
  14. Written by:    Cameron Birse                                        August 1988
  15.  
  16. This Technical Note explains how the Apple Desktop Bus (ADB) works on the Macintosh.  This Note covers the boot process, driver installation, use of ADB Manager calls, and answers commonly asked questions.
  17. Changes since August 1989:  Added an additional vendor for ADB cables.
  18. _______________________________________________________________________________
  19.  
  20.  
  21. Boot Process
  22.  
  23. During the boot process, the ADB Manager finds all the devices on the bus and resolves any address conflicts.  An address conflict is defined as two or more devices with the same original (default) address.  A good example of this conflict is a mouse and a graphics tablet that are both at address 3 (relative device).  The ADB Manager resolves these address conflicts as described in Appendix B of the ADB Specification (Apple Drawing #062-0267-E) and the Q & A section of this document.
  24.  
  25. After the address resolution, the devices which have been ΓÇ£movedΓÇ¥ due to address conflicts are now addressed, starting from the highest unused soft address and working down.  The system now loads and executes all the resources of type 'ADBS' that match the devices on the bus (by original address).
  26.  
  27. Once all the ADB service routines are installed, the ADB transceiver
  28. (microcontroller) chip starts polling the active device.  The active device is defined as the last device to send data.  Since the mouse (pointing device) is the most likely device to have data ready at any given time, it defaults as the active device after startup.
  29.  
  30. The transceiver polls the active device (approximately every 11 milliseconds), with a Talk R0 command.  If the active device has new data, it can respond with it, and if it does not, it just times out.  If any other devices have data to send, they can assert SRQ (refer to Figure 5 of the ADB Specification) at the end of the talk R0 command.  When the host detects an SRQ, it begins polling all addresses with a talk R0 command until one returns data.  That device then becomes the active device.
  31.  
  32. Devices have no way of knowing if they are the ΓÇ£active deviceΓÇ¥. The algorithm for a device with data ready to send is as follows:
  33.  
  34.   ΓÇó  Wait for a talk R0 command (this happens every 10 milliseconds).
  35.   ΓÇó  If the Talk R0 is for you, then return the data.
  36.   ΓÇó  If the Talk R0 is not for you, wait for the end of the command,
  37.      and assert SRQ.
  38.   ΓÇó  If the Talk R0 is addressed to you, then respond with your data.
  39.  
  40. Now that a device has been polled, the host retrieves the data from the bus and calls the service routine installed for that device (service routines are installed by calling _SetADBInfo and are maintained by the ADB Manager).  The system passes pointers to the service routine itself, its data area, and the data received from the device, as well as the ADB command byte that caused the routine to be called.
  41.  
  42. Normally, the service routine does not need to use the _ADBOp call to retrieve data.  The ADB ΓÇ£philosophyΓÇ¥ assumes that register zero of a device is the main data transmission register.  Since register zero is automatically polled by the system, there should be no need to call _ADBOp from the service routine.  Typically, _ADBOp is used to set modes of a device, or to interrogate the device for statusΓÇöthe sort of things that should not need to be done more than once or twice during normal operation.
  43.  
  44. It is important to note that ADB service routines are called at interrupt time, which means that they must follow all the rules regarding code that executes at interrupt time.  (See Inside Macintosh references to VBL tasks and Device Manager I/O completion routines.)
  45.  
  46.  
  47. Installing an ADB Service Routine and Optional Data Area
  48.  
  49.   ΓÇó  Using the 'ADBS' resource mechanism.  At boot time the system searches
  50.      for 'ADBS' resources in the System file.  The system matches desktop
  51.      bus devices by their original address to an 'ADBS' resource (i.e., if
  52.      the machine has a device that responds at address 4, the system looks
  53.      for an 'ADBS' resource with ID=4).  When the system finds these resources,
  54.      it loads and executes them.  The limitation of this method is that there
  55.      can only be one 'ADBS' resource for each address on the bus.
  56.  
  57.      A typical 'ADBS' resource allocates space in the system heap for its
  58.      service routine and, optional, data area.  Next, it moves the service
  59.      routine into the allocated space and initializes the data area, if
  60.      necessary.  This code should also install an _ADBReInit preprocessing
  61.      routine to deallocate the memory used by the service routine (Inside
  62.      Macintosh V-367).
  63.  
  64.      When the system loads and executes an 'ADBS' resource, it passes the
  65.      following parameters:
  66.  
  67.      A0 = Address of 'ADBS' resource in memory.
  68.      D0 = ADB device address (0-15).  This address may be different than
  69.           the ΓÇ£original address,ΓÇ¥ since it occurs after address resolution.
  70.      D1 = ADB Device Type (same as the handler ID)
  71.  
  72.      With this information, the 'ADBS' code can call _SetADBInfo to install
  73.      the service routine and data area.  The installer should make sure the
  74.      handler ID (Device Type) is the one it expects.
  75.  
  76.   ΓÇó  Install the service routine and data area using an 'INIT' resource.
  77.      This method works the same as an 'ADBS' resource, except you are not
  78.      passed the parameters in A0, D0, and D1, so you at least need to
  79.      interrogate the ADB Manager (using _GetIndADB) to get the information.
  80.      Remember to lock yourself down.
  81.  
  82.   ΓÇó  Use the 'INIT' resource method from within an application.
  83.  
  84.  
  85. Answers to Commonly-Asked ADB Questions
  86.  
  87. Question:  I need information on developing an Apple Desktop Bus product.
  88. Answer:    AppleΓÇÖs Desktop Bus and ADB Device Specifications are a licensable
  89.            product available through Software Licensing.  For more information,
  90.            contact:
  91.  
  92.                            Apple Software Licensing
  93.                            Apple Computer, Inc.,
  94.                            20525 Mariani Avenue, M/S 38-I
  95.                            Cupertino, CA, 95014
  96.                            (408) 974-4667
  97.                            AppleLink:  SW.LICENSE
  98.  
  99.            Additional ADB references are as follows:
  100.  
  101.            Macintosh
  102.                Inside Macintosh, Volume V, The Apple Desktop Bus
  103.                Macintosh Family Hardware Reference
  104.  
  105.            Apple II
  106.                Apple IIgs Hardware Reference Manual
  107.  
  108.            Desktop Bus
  109.                Apple IIgs Firmware Reference Manual
  110.  
  111.            General
  112.                Baum, Peter. ΓÇ£Boarding the Bus,ΓÇ¥ MacUser, July 1987, p. 142.
  113.                             ΓÇ£An Overview of Apple Desktop Bus,ΓÇ¥
  114.                             Call A.P.P.L.E., June 1987, p. 24.
  115.  
  116. Question:  I would like to extend the keyboard cable for my Macintosh.
  117.            How can I do this, and how can I make the extension?
  118. Answer:    The ADB specification states the maximum length of all cables
  119.            on the Desktop Bus is five meters.  If you wish to use longer
  120.            cables than those supplied with the ADB device, Kensington
  121.            MicroWare (800) 535-4242, Monster Cable (800) 331-3755, and
  122.            Data Spec (800) 431-8124 all supply them.
  123.  
  124.            Disclaimer:  This listing for Kensington MicroWare, Monster
  125.                         Cable, and Data Spec neither implies nor constitutes
  126.                         an endorsement by Apple Computer, Inc.  If your
  127.                         company supplies these cables and you would like
  128.                         to be listed, contact us at the address in Technical
  129.                         Note #0.
  130.  
  131. Question:  How can I use the LEDs on the Apple Extended Keyboard?
  132. Answer:    Using the LEDs on the extended keyboard involves the _ADBOp
  133.            call.  Once you determine that you have an extended keyboard
  134.            (with _CountADBs and _GetIndADB), then register 2 of the extended
  135.            keyboard has the LED toggles in the low 3 bits.
  136.  
  137.            Therefore, you would do a Talk to register 2 to have the device
  138.            send you the contents of register 2, manipulate the low three
  139.            bits to set the LEDs, and then pass the modified register 2 back
  140.            to the device with a Listen to register 2 command.
  141.  
  142.            The Apple Extended Keyboard has an ID of 02 and a device handler
  143.            ID of 02, while the Apple Standard Keyboard has an ID of 02 and
  144.            a device handler ID of 01.
  145.  
  146.            Note:  At this point it is not clear what Apple has in mind for
  147.                   these LEDs, so you are using them at your own risk.
  148.  
  149. Question:  I am confused about the service routines and data areas passed
  150.            in the _ADBOp call.  What does it all mean?
  151. Answer:    That ΓÇÖs a good question.
  152.  
  153.            FUNCTION ADBOp (data:Ptr; compRout:ProcPtr; buffer:Ptr;
  154.                           commandNum:INTEGER) : oserr;
  155.  
  156.            data          is a pointer to the ΓÇ£optional data areaΓÇ¥.  This
  157.                          area is provided for the use of the service routine
  158.                          (if needed).
  159.            compRout      is a pointer to the completion or service routine
  160.                          to be called when the _ADBOp command has been
  161.                          completed.  It has the same meaning as the service
  162.                          routine passed to the _SetADBInfo call.
  163.            buffer        is a pointer to a Pascal string, which may contain
  164.                          zero to eight bytes of information.  These are the
  165.                          two to eight bytes that a particular register of an
  166.                          ADB device is capable of sending and receiving.
  167.            commandNum    is an integer that describes the command to be
  168.                          sent over the bus.
  169.  
  170.            There is some confusion over the way that the completion routines
  171.            are called from _ADBOp.  This calling may be done in one of the
  172.            following three ways:
  173.  
  174.            ΓÇó  You do not wish to have a completion routine called, as in
  175.               a Listen command.  Pass a NIL pointer to _ADBOp.
  176.            ΓÇó  You wish to call the routine already in use by the system
  177.               for that address (as installed by _SetADBInfo).  Call
  178.               _GetADBInfo before calling _ADBOp, and pass the routine
  179.               pointer returned by _GetADBInfo to _ADBOp.
  180.            ΓÇó  You wish to provide your own completion routine and data
  181.               area for the _ADBOp call.  In this case, simply pass your
  182.               own pointers to the _ADBOp call.
  183.  
  184.            Remember, there should rarely be a reason to call _ADBOp.  Most
  185.            cases are handled by the systemΓÇÖs polling and service request
  186.            mechanism.  In the cases where it is necessary to call _ADBOp,
  187.            it should not be done in a polling fashion, but as a mechanism
  188.            of telling the device something (i.e., change modes, or in the
  189.            case of our extended keyboard, turn on or off an LED).
  190.  
  191. Question:  How can I make my Macintosh II or IIx power up automatically
  192.            after a power outage?
  193. Answer:    The Macintosh II and IIx power can be turned on via the keyboard
  194.            through the Apple Desktop Bus port (ADB) since the reset key is
  195.            wired to pin two of the ADB connector.  When you press this key,
  196.            it pulls pin two to ground and initiates a power-on sequence.
  197.            You can emulate this feature with a momentary switch connected
  198.            to the ADB port.  Note that the switch on the back panel of a
  199.            Macintosh IIcx can be locked in the On position to automatically
  200.            restart after a power outage
  201.  
  202.            An idea for a power-on circuit would be to have a momentary
  203.            (one-shot) relay powered by the same outlet that powers the
  204.            machine and have the contacts close pin two of the ADB connector.
  205.            (Without having tried this, I am concerned that you may need a
  206.            delay before the relay fires to give the AC time to stabilize, etc.)
  207.  
  208. Question:  IΓÇÖm more than a little confused about the way ADB device address
  209.            conflicts are resolved at boot time.  Can you enlighten me with
  210.            your infinite wisdom, Cameron?
  211. Answer:    The method used by the host to separate and identify the devices
  212.            at boot time is not well documented, so IΓÇÖll try to describe it
  213.            with some clarity.
  214.  
  215.            The host issues a Talk R3 command to an address.  LetΓÇÖs say there
  216.            are two devices at that address.  Both try to respond to the
  217.            command, and when they try to put the random number (the address
  218.            field of register 3) on the bus, one of them should detect a
  219.            collision.  The one that detects the collision backs off and marks
  220.            itself (internally) as unmovable.
  221.  
  222.            The device that did respond successfully is then told to move to
  223.            a new address (the highest free address).  By definition, moving
  224.            to a new address means that it now responds only to commands
  225.            addressed to this new address, and it ignores commands to the
  226.            original address.
  227.  
  228.            The host then issues another Talk R3 command to the original
  229.            address.  This time the second device responds without detecting
  230.            a collision.  When it successfully completes a Talk R3 response, 
  231.            it marks itself as movable.  It then is told to move to a new
  232.            address.
  233.  
  234.            The host again issues a Talk R3 command to the original address.
  235.            Since there are no more devices at that address, the bus times
  236.            out, and the host moves the last device back to the original address.
  237.  
  238.            At this point, the host moves up to the next address that has a
  239.            device and begins the process all over.
  240.  
  241.            Generally, when having trouble separating devices on the ADB, it
  242.            is because the collision detection doesnΓÇÖt work well.  In fact,
  243.            this problem is evident on Apple keyboards.  The bug is that the
  244.            random number returned in R3 isnΓÇÖt really a random number.  Since
  245.            the microcontrollers on the keyboards are clocked with a crystal,
  246.            they tend to generate the same ΓÇ£randomΓÇ¥ number, so when the system
  247.            attempts to separate them with a Talk R3 command, they never detect
  248.            the collision.
  249.  
  250.            One possible solution is to use a low-tolerance capacitor on the
  251.            reset line of the microcontroller, thereby forcing the time from
  252.            power on to the time reset is negated to be fairly random.  In this
  253.            way, the microcontroller can start a count until it receives the
  254.            first Talk R3 command, and hopefully it is a different number than
  255.            another device at the same address on the bus.
  256.  
  257.            If you find your device shows up at all addresses, it may be
  258.            because it is responding to the move address command when it should
  259.            be marked as unmovable.
  260.  
  261.            Finally, if the device doesnΓÇÖt show up at all, it may be because
  262.            it is unable to respond to the Talk R3 command at boot time
  263.            (i.e., not able to initialize itself and start watching the bus
  264.            in time).
  265.  
  266.  
  267. Further Reference:
  268. _______________________________________________________________________________
  269.   ΓÇó  Inside Macintosh, Volume V-361, Apple Desktop Bus
  270.   ΓÇó  Macintosh Family Hardware Reference, Chapters 11 & 19
  271.   ΓÇó  Technical Note #160, Key Mapping
  272.   ΓÇó  MacDTS Sample Code #17, TbltDrvr
  273.  
  274.  
  275. -- part contents for background part 2
  276. ----- text -----
  277. 206
  278.  
  279. -- part contents for background part 7
  280. ----- text -----
  281. Space Aliens Ate My Mouse
  282.  
  283. -- part contents for background part 113
  284. ----- text -----
  285. Apple Desktop Bus
  286. Technical Note #160